//
//  InputIpPortView.m
//  TestTcpUdp
//
//  Created by leeson zhong on 2021/7/22.
//

#import "InputIpPortView.h"

//数字
#define PATTERN_PORT @"\\d{1,5}"
//ip地址
#define PATTERN_IP @"([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"

#define DEF_INPUT_IP @"1234567890."
#define DEF_INPUT_PORT @"1234567890"

@interface InputIpPortView()<UITextFieldDelegate>{
    
}
@property (weak, nonatomic) IBOutlet UITextField *ipTextField;
@property (weak, nonatomic) IBOutlet UITextField *portTextField;
@property (weak, nonatomic) IBOutlet UIView *ipView;

@end

@implementation InputIpPortView


- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self initTextField];
    
}

-(void)initTextField{
    self.ipTextField.delegate = self;
    self.portTextField.delegate = self;
    if (self.showIP) {
        self.ipView.hidden = NO;
    }else{
        self.ipView.hidden = YES;
    }
}

- (void)setShowIP:(BOOL)showIP{
    _showIP = showIP;
    if (showIP) {
        self.ipView.hidden = NO;
    }else{
        self.ipView.hidden = YES;
    }
}


- (IBAction)touchUpCancelButton:(id)sender {
    if (self.delegate) {
        [self.delegate onInputIpPortViewCancel];
    }
}

- (IBAction)touchUpComfirmButton:(id)sender {
    NSString *ip = self.ipTextField.text;
    NSString * port = self.portTextField.text;
    if (self.delegate) {
        [self.delegate onInputIpPortViewComfirmIp:ip Port:port];
    }
}

+(BOOL)isIp:(NSString *)ip{
    NSString *result = ip;

    if (!result) return NO;
    
    NSString *pattern = PATTERN_IP;        //匹配规则
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    
    NSTextCheckingResult *matchResult = [regex firstMatchInString:result options:0 range:NSMakeRange(0, [result length])];

    if (matchResult) {
        return  YES;
    }else{
        return NO;
    }
}
+(BOOL)isPort:(NSString *)port{
    NSString *result = port;
        
    if (!result) return NO;
    
    NSString *pattern = PATTERN_PORT;        //匹配规则
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
    
    NSTextCheckingResult *matchResult = [regex firstMatchInString:result options:0 range:NSMakeRange(0, [result length])];
    
    if (matchResult) {
        return  YES;
    }else{
        return NO;
    }
}


//--------------------------UITextFieldDelegate-------------------------
//限制输入字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.ipTextField == textField) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_INPUT_IP]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest){
            return NO;
        }
        
    }else if (self.portTextField == textField) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:DEF_INPUT_PORT]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest){
            return NO;
        }
    }
    
    return YES;
}
//按return退出键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return NO;
}
@end
